package com.fa4j.third.base.config;

import com.fa4j.common.base.service.CommonSettingsService;
import com.fa4j.third.base.service.ThirdConfigService;
import com.fa4j.third.base.service.impl.ThirdCommonConfigServiceImpl;
import com.fa4j.third.base.service.impl.ThirdFileConfigServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;


@Slf4j
@AutoConfiguration
@EnableConfigurationProperties(ThirdConfig.class)
public class ThirdCommonAutoConfiguration {


    @Bean
    @ConditionalOnBean(CommonSettingsService.class)
    @ConditionalOnProperty(value = "fa4j.third.config", havingValue = "true", matchIfMissing = true)
    public ThirdConfigService thirdCommonConfigService(CommonSettingsService commonSettingsService) {
        return new ThirdCommonConfigServiceImpl(commonSettingsService);
    }

    @Bean
    @ConditionalOnMissingBean
    public ThirdConfigService thirdFileConfigService(ThirdConfig thirdConfig) {
        return new ThirdFileConfigServiceImpl(thirdConfig);
    }

}
